<?php
namespace Home\Model;
use Think\Model;
class TUserModel extends Model
{
	private $select_fields = null;
	private $fields_map = false;
	private $value_map = null;
	private $role_auth = null;
	public function fields($fields){
		// 不能直接用$this赋值。。不然无效
		list($select_fields,$fields_map) = isIndex($fields);	
		$this->select_fields = $select_fields;
		$this->fields_map = $fields_map;
		return $this;
	}
	public function useValueMap($map_array){
		$this->value_map = $map_array;
		return $this;
	}
    private function _getConfig($data){
		$fields = $this->select_fields;
		$fields_map = $this->fields_map;
		$value_map = $this->value_map;
		$status_key = $this->status_key;
		$status_value = $this->status_value;
		$this->select_fields = null;
		$this->fields_map = false;
		$this->value_map = null;
		$this->status_key = '';
		$this->status_value = '';
		$data = $fields?getArrayFields($data,$fields,$fields_map,$status_key,$status_value):$data;
    	return $value_map?arrayMap($data,$value_map):$data;
    }
	public function getUserList($roleids)
	{
		$all_user = $this->where(['role_id'=>['in',$roleids]])->select();
		return $this->_getConfig($all_user);
	}
	public function checkAuth($auth){
		$this->role_auth = $auth;
		return $this;
	}
	private function _checkAuth($id){
		$auth = $this->role_auth;
		if(!$auth) return false;
		$this->role_auth = null;
		return in_array($id, $auth);
	}
	public function checkStatus($status_key,$status_value){
		$this->status_key = $status_key;
		$this->status_value = $status_value;
		return $this;
	}
	public function getInfo($id){
		$info = $this->where(['id'=>$id])->select();
		$info = $fields?getArrayFields($info,$fields,$map):$info;
		if( !$this->_checkAuth($info[0]['role_id'])){
			return false;
		}
		$info = $this->_getConfig($info);
		return $info[0];
	}
}